package com.jinglvhuanjing.app.utils;

import android.app.Activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Stack;


public class AppManager {

    private static volatile AppManager instance;
    private Stack<Activity> activities;
    private Activity mMainActivity;

    public static AppManager newInstance() {
        if (instance == null) {
            synchronized (AppManager.class) {
                if (instance == null) {
                    instance = new AppManager();
                }
            }
        }
        return instance;
    }

    private AppManager() {
    }

    /**
     * 当前堆栈是否为空？
     */
    public boolean isEmpty() {
        return (null == activities || activities.isEmpty());
    }

    /**
     * 获取堆栈中Activity的数量
     */
    private int size() {
        return null == activities ? 0 : activities.size();
    }

    /**
     * 获取堆栈中所有的Activity
     */
    @Nullable
    public Stack<Activity> getActivityList() {
        return activities;
    }

    /**
     * 添加Activity
     */
    public void addActivity(@NonNull Activity activity) {
        if (null == activities) {
            activities = new Stack<>();
        }
        activities.add(activity);
    }

    /**
     * 添加首页Activity
     */
    public void setMainActivity(@NonNull Activity activity) {
        this.mMainActivity = activity;
    }

    @NonNull
    public Activity getMainActivity() {
        return mMainActivity;
    }

    /**
     * 销毁MainActivity
     */
    public void finishMainActivity() {
        if (mMainActivity != null) {
            mMainActivity.finish();
            mMainActivity = null;
        }
    }

    /**
     * 获取当前Activity：堆栈最上层的Activity
     */
    public Activity currentActivity() {
        return isEmpty() ? getMainActivity() : activities.lastElement();
    }

    /**
     * 结束当前Activity：堆栈最上层的Activity
     */
    public void finishCurrentActivity() {
        Activity activity = currentActivity();
        if (null != activity) {
            this.finish(activity);
        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        if (!this.isEmpty()) {
            for (Activity activity : activities) {
                activity.finish();
            }
            activities.clear();
        }
    }

    /**
     * 结束指定Activity
     *
     * @param clazz 指定需要关闭的Activity
     */
    public void finish(@NonNull Class<?> clazz) {
        if (!isEmpty()) {
            for (Activity activity : activities) {
                if (activity.getClass().equals(clazz)) {
                    this.finish(activity);
                    break;
                }
            }
        }
    }

    /**
     * 结束指定Activity
     *
     * @param className 指定需要关闭的Activity 类名
     */
    public void finish(String className) {
        if (!isEmpty()) {
            for (Activity activity : activities) {
                if (activity.getClass().getSimpleName().equals(className)) {
                    this.finish(activity);
                    break;
                }
            }
        }
    }

    /**
     * 结束指定Activity
     *
     * @param activity {@link Activity}
     */
    public void finish(@NonNull Activity activity) {
        this.remove(activity);
        activity.finish();
    }

    /**
     * 从堆栈中移除某个Activity，但并不关闭它
     *
     * @param activity {@link Activity}
     */
    public void remove(@NonNull Activity activity) {
        if (!this.isEmpty()) {
            activities.remove(activity);
        }
    }

    /**
     * Activity是否在运行
     *
     * @param activity {@link Activity}
     */
    private boolean isRun(@NonNull Activity activity) {
        return !(activity.isDestroyed() || activity.isFinishing());
    }

    /**
     * 根据class 获取activity实列
     *
     * @param clazz {@link Class}
     * @return Activity
     */
    public Activity findActivityByClass(Class<?> clazz) {
        if (activities == null || activities.isEmpty()) {
            return mMainActivity;
        }
        for (Activity activity : activities) {
            if (activity.getClass() == clazz) {
                return activity;
            }
        }
        return mMainActivity;
    }

    /**
     * 根据class 判断是否存在
     *
     * @param clazz {@link Class}
     * @return boolean
     */
    public boolean hasActivityByClass(Class<?> clazz) {
        if (activities == null || activities.isEmpty()) {
            return false;
        }
        for (Activity activity : activities) {
            if (activity.getClass() == clazz) {
                return true;
            }
        }
        return false;
    }

    /**
     * 根据class 名字获取activity实列
     *
     * @param className 类名
     * @return Activity
     */
    public Activity findActivityByClassName(String className) {
        if (activities == null || activities.isEmpty()) {
            return mMainActivity;
        }
        for (Activity activity : activities) {
            if (activity.getClass().getSimpleName().equals(className)) {
                return activity;
            }
        }
        return mMainActivity;
    }

    /**
     * 获取上一个Activity的实例（倒数第二）
     *
     * @return Activity
     */
    @Nullable
    public Activity findSecondLastActivity() {
        final int size = this.size();
        if (size >= 2) {
            return activities.get(size - 2);
        }
        return null;
    }
}